<?php
/**
 * Created by dutuwang.net
 * User: adophper <hello@adophper.com>
 * Date: 2018/4/2
 * Time: 18:03
 */
?>
<div class="category-row">
    <!--<div class="form-group">
        <div class="input-group">
            <input type="text" class="form-control" id="dialog_category" value="">
            <span class="input-group-addon">搜索</span>
        </div>
    </div>-->
    <p><label class="label label-warning">已选择</label>：代表已选择；<label class="label label-primary">未选择</label>：代表未选择。</p>
    <p>&nbsp;</p>
    <div class="category-list">
        <div class="">
        <?php
        if (!empty($category)) {
            foreach ($category as $val) {
        ?>
        <label class="select-category label label-<?=$val['id']==$categoryId?'warning':'primary'?>" data-id="<?=$val['id']?>" ><?=$val['name']?></label>
        <?php
            }
        }
        ?>
        </div>
    </div>
</div>
<script>
    $(function () {
        $('.select-category').unbind('click').bind('click', function (e) {
            //$(".select-category").removeClass('label-warning').addClass('label-primary');
            if ($(this).hasClass('label-warning')){
                $(this).removeClass('label-warning');
                $(this).addClass('label-primary');
            }else{
                /*if ($(".category-list").find('.label-warning').length >= 5) {
                    alert('最多只能选择5个行业');
                    return;
                }*/
                $(".select-category").removeClass('label-warning').addClass('label-primary');
                $(this).addClass('label-warning');
                $(this).removeClass('label-primary');
            }
        })
        $('#formModal .btn-primary').unbind('click').bind('click', function (e) {
            var categoryId = $(".select-category.label-warning").data('id');
            var categoryName = $(".select-category.label-warning").text();
            if (typeof categoryId == 'undefined') {
                categoryId = '';
                categoryName = '';
            }
            $("#category_id").val(categoryId);
            $("#category_name").val(categoryName);
            $("#formModal").modal('hide');
        })
    })
</script>